<?php
/**
* VIDEO CONFERENCES
* Copyright (c) 2011 
* Author: Dmitriy Tovkach (ragvalod@gmail.com)
* @license		GNU/GPL
**/
class VideoSocial extends CApplications

{
	function VideoSocial()
	{
		
		
		
		$config	=& CFactory::getConfig();
		
		
		//$this->loadUserParams();
		
		$uri		= JURI::base();
		
		$user		=& CFactory::getActiveProfile();
		$document	=& JFactory::getDocument();
		return 0;
		
	}
	function GetUserNameAndLocation($id = null)
	{
		//$my = $this->loadUserParams();
		if ($id)
		{
			$my =& CFactory::getUser($id);	
		} else
		{
			$my =& CFactory::getUser();
		}
		
		
		$country = $my->getInfo('FIELD_COUNTRY');
		$display_name = $my->getDisplayName();
		if ($country != "")
		{
		   return $display_name.', '.$country;	
		} else
		{
		   return $display_name;
		}
			
	}
	function GetVideoconference($id)
	{
		
		//$user =& CFactory::getActiveProfile();
		//$events = CFactory::getModel('events');
		//$events_by_user = $events->getEvents(0,85);
		//print_r($events_by_user);
		
		$db		=&	JFactory::getDBO();

		$query	=	'SELECT b.*'
					. ' FROM '
					//. $db->nameQuote("#__community_events_members") . ' AS a, '
					. $db->nameQuote("#__community_events") . ' AS b'
					. ' WHERE b.' . $db->nameQuote('id').'=' . $db->Quote($id);
					//. ' AND b.' . $db->nameQuote('id').'=' . $db->nameQuote(20)
					//. ' AND b.' . $db->nameQuote('published').'=' . $db->Quote( 1 )
					//. ' AND a.' . $db->nameQuote('status').'=' . $db->Quote( COMMUNITY_EVENT_STATUS_INVITED )					
					//. ' AND b.' . $db->nameQuote('enddate').'>= NOW()'
					//. ' ORDER BY a.' . $db->nameQuote('id').' DESC';
					//. " LIMIT {$limitstart}, {$limit}";
					
		$db->setQuery($query);

		if( $db->getErrorNum() ){
			JError::raiseError(500, $db->stderr());
		}

		$result = $db->loadObjectList();
		return $result[0];
		
	}
	function EndVideoconference($id)
	{
		$my =& CFactory::getUser();
		$videoconference = $this->GetVideoconference($id);
		if ($my->id == $videoconference->creator)
		{
			
			$db		=&	JFactory::getDBO();
			$query	=	'UPDATE '. $db->nameQuote("#__community_events") . ' set '
					. $db->nameQuote('published').'=0'
					//. $db->nameQuote("#__community_events_members") . ' AS a, '
					//. $db->nameQuote("#__community_events") . ' AS b'
					. ' WHERE ' . $db->nameQuote('id').'=' .$db->Quote($id);
					//. ' AND b.' . $db->nameQuote('id').'=' . $db->nameQuote(20)
					//. ' AND b.' . $db->nameQuote('published').'=' . $db->Quote( 1 )
					//. ' AND a.' . $db->nameQuote('status').'=' . $db->Quote( COMMUNITY_EVENT_STATUS_INVITED )					
					//. ' AND b.' . $db->nameQuote('enddate').'>= NOW()'
					//. ' ORDER BY a.' . $db->nameQuote('id').' DESC';
					//. " LIMIT {$limitstart}, {$limit}";
					
			$db->setQuery($query);
			$db->query();
			if( $db->getErrorNum() ){
				JError::raiseError(500, $db->stderr());
			}

		//$result = $db->loadObjectList();
		return $db->_errorNum;
		}
	}
	function SetActiveVideoconference($id)
	{
		$my =& CFactory::getUser();
		

			$db		=&	JFactory::getDBO();
			$query	=	'UPDATE '. $db->nameQuote("#__community_events") . ' set '
					. $db->nameQuote('created').'="'.date('Y-m-d H:i:s').'" '
					//. $db->nameQuote('offset').'=-1 '
					//. $db->nameQuote("#__community_events_members") . ' AS a, '
					//. $db->nameQuote("#__community_events") . ' AS b'
					. ' WHERE ' . $db->nameQuote('id').'=' . $id;
					//. ' AND b.' . $db->nameQuote('id').'=' . $db->nameQuote(20)
					//. ' AND b.' . $db->nameQuote('published').'=' . $db->Quote( 1 )
					//. ' AND a.' . $db->nameQuote('status').'=' . $db->Quote( COMMUNITY_EVENT_STATUS_INVITED )					
					//. ' AND b.' . $db->nameQuote('enddate').'>= NOW()'
					//. ' ORDER BY a.' . $db->nameQuote('id').' DESC';
					//. " LIMIT {$limitstart}, {$limit}";
					
			$db->setQuery($query);
			$db->query();
			if( $db->getErrorNum() ){
				JError::raiseError(500, $db->stderr());
			}

		
		return $db->_errorNum;
		
	}
	function GetListFriends()
	{
		$my =& JFactory::getUser();
		$user =& CFactory::getActiveProfile();
		$friends = CFactory::getModel( 'friends' );		
		
		$rows = $friends->getFriends( $my->id , $sorted , true , $filter );
                $out_friends = array();
		$count = 0;
		foreach($rows as $val)
		{
		   $friend = & CFactory::getUser($val->id);	   
		   if ($friend->isOnline() == 1)
		   {
			$out_friends[$count]['id'] = $val->id;
			$out_friends[$count]['name'] = $friend->getDisplayName();
			$count++;
		   }
		  		   
		}
		return $out_friends;
	}
	
	

}
?>